<template>
  <div class="photo-view">
    <el-card>
      <template #header>
        <div class="card-header">
          <span>新闻照片管理</span>
          <el-input
            v-model="searchQuery"
            placeholder="请输入关键词搜索"
            style="width: 300px"
            @keyup.enter="handleSearch"
          >
            <template #append>
              <el-button :icon="Search" @click="handleSearch" />
            </template>
          </el-input>
        </div>
      </template>

      <PhotoList
        ref="photoListRef"
        @create="showCreateForm"
        @edit="showEditForm"
      />

      <PhotoForm
        v-model:visible="formVisible"
        :photo-id="currentPhotoId"
        @success="handleSuccess"
      />

      <PhotoDetail ref="photoDetailRef" />
    </el-card>
  </div>
</template>

<script setup>
import { ref } from 'vue'
import { Search } from '@element-plus/icons-vue'
import PhotoList from '@/components/PhotoList.vue'
import PhotoForm from '@/components/PhotoForm.vue'
import PhotoDetail from '@/components/PhotoDetail.vue'
import { usePhotoStore } from '../stores/photoStore'

const photoStore = usePhotoStore()
const photoListRef = ref(null)
const photoDetailRef = ref(null)
const searchQuery = ref('')
const formVisible = ref(false)
const currentPhotoId = ref(null)

const showCreateForm = () => {
  currentPhotoId.value = null
  formVisible.value = true
}

const showEditForm = (id) => {
  currentPhotoId.value = id
  formVisible.value = true
}

const handleSuccess = () => {
  photoListRef.value?.fetchPhotos()
}

const handleSearch = () => {
  if (searchQuery.value) {
    photoStore.searchPhotos({ keyword: searchQuery.value })
  } else {
    photoStore.fetchPhotos()
  }
}
</script>

<style scoped>
.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
}
</style>